package com.hengxinguotong.zhihuichengjian.widget.datepicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.hengxinguotong.zhihuichengjian.R;

import java.util.Calendar;

public class DatePicker extends LinearLayout {

	private Calendar calendar = Calendar.getInstance();
	private WheelView years, months, days;
	private OnChangeListener onChangeListener;

	// Constructors
	public DatePicker(Context context) {
		super(context);
		init(context);
	}

	public DatePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {

		years = new WheelView(context);
		LayoutParams lparams_hours = new LayoutParams(160,
				LayoutParams.MATCH_PARENT);
		lparams_hours.setMargins(0, 5, 16, 5);
		years.setLayoutParams(lparams_hours);
		years.setAdapter(new NumericWheelAdapter(1900, 2100));
		years.setVisibleItems(3);
		years.addChangingListener(onYearsChangedListener);
		addView(years);
		addView(LayoutInflater.from(context).inflate(R.layout.line,
				null));
		months = new WheelView(context);
		LayoutParams lparams_month = new LayoutParams(120,
				LayoutParams.MATCH_PARENT);
		lparams_month.setMargins(0, 5, 16, 5);
		months.setLayoutParams(lparams_month);
		months.setAdapter(new NumericWheelAdapter(1, 12, "%02d"));
		months.setVisibleItems(3);
		months.setCyclic(true);
		months.addChangingListener(onMonthsChangedListener);
		addView(months);
		addView(LayoutInflater.from(context).inflate(R.layout.line,
				null));
		days = new WheelView(context);
		days.setLayoutParams(new LayoutParams(120, LayoutParams.MATCH_PARENT));
		int maxday_of_month = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		days.setAdapter(new NumericWheelAdapter(1, maxday_of_month, "%02d"));
		days.setVisibleItems(3);
		days.setCyclic(true);
		days.addChangingListener(onDaysChangedListener);
		addView(days);
	}

	// listeners
	private OnWheelChangedListener onYearsChangedListener = new OnWheelChangedListener() {
		@Override
		public void onChanged(WheelView hours, int oldValue, int newValue) {
			calendar.set(Calendar.YEAR, 1900 + newValue);
			onChangeListener.onChange(getYear(), getMonth(), getDay(),
					getDayOfWeek());
			setMonth(getMonth());
			setDay(getDay());
		}
	};
	private OnWheelChangedListener onMonthsChangedListener = new OnWheelChangedListener() {
		@Override
		public void onChanged(WheelView mins, int oldValue, int newValue) {
			calendar.set(Calendar.MONTH, 1 + newValue - 1);
			onChangeListener.onChange(getYear(), getMonth(), getDay(),
					getDayOfWeek());
			setMonth(getMonth());
			setDay(getDay());
		}
	};
	private OnWheelChangedListener onDaysChangedListener = new OnWheelChangedListener() {
		@Override
		public void onChanged(WheelView mins, int oldValue, int newValue) {
			calendar.set(Calendar.DAY_OF_MONTH, newValue + 1);
			onChangeListener.onChange(getYear(), getMonth(), getDay(),
					getDayOfWeek());
			days.setAdapter(new NumericWheelAdapter(1, calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH), "%02d"));
		}
	};

	public interface OnChangeListener {
		void onChange(int year, int month, int day, int day_of_week);
	}

	public void setOnChangeListener(OnChangeListener onChangeListener) {
		this.onChangeListener = onChangeListener;
	}

	public void setYear(int year) {
		years.setCurrentItem(year - 1900);
	}

	public int getYear() {
		return calendar.get(Calendar.YEAR);
	}

	public void setMonth(int month) {
		months.setCurrentItem(month - 1);
	}

	public int getMonth() {
		return calendar.get(Calendar.MONTH) + 1;
	}

	public void setDay(int day) {
		days.setCurrentItem(day - 1);
	}

	public int getDay() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public int getDayOfWeek() {
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setYear(getYear());
		setMonth(getMonth());
		setDay(getDay());
	}
}
